requireJS(['constant', 'site', 'handlebars', 'static/js/page/common/common.js'], function (Constant, site, Handlebars, Common) {
    Handlebars.registerHelper('state', function (v1, v2, opts) {
        if (v1 == v2)
            return opts.fn(this);
        else
            return opts.inverse(this);
    });
    var frontData = {
        pageNum: 1,
        state: 1
    }
    var updateData = {};//作废编号
    var recoverData = {};//恢复编号
    var order_id;
    var product_id;
    var status;
    var all_select = function () {

        site.send({
            url: "manager/selectPageContractNo.do",
            //async:false
            data: frontData,
            callback: function (data) {
                var list = JSON.parse(data)
                if (list.type == '1') {
                    $("#total").text(list.content.total)
                    var source = $("#contract_num_management_tpl").html();
                    var template = Handlebars.compile(source);

                    var menuIndex = getAurlparam('h') || 0;
                    var thirdMenusIndex = getAurlparam('t') || 0;
                    var leftIndex = getAurlparam('i') || 0;

                    var storageContent = JSON.parse(localStorage.getItem('content'));

                    var obj = {
                        content: list,
                        fourthMenus: storageContent.menus[menuIndex].children[leftIndex].children[thirdMenusIndex].children || {}
                    }

                    var html = template(obj);
                    $("#contract_num_management").html(html);

                    var source = $("#special_fourth_tpl").html()
                    var template = Handlebars.compile(source)
                    var html = template(obj.fourthMenus)
                    $("#addList").remove();
                    $('.public-botPage-left').append(html)

                    // 分页
                    $('.contract-page-container').createPager({
                        current: frontData.pageNum,
                        total: list.content.total,
                        pageSize: list.content.pageSize,
                        needInput: false,
                        backFn: function (current) {
                            frontData.pageNum = current;
                            all_select();
                        }
                    })
                } else {
                    Constant.fn.Message('F', list.msg);
                }
                onBindFun();
            }
        })


    }
    all_select();

    function onBindFun() {
        $("#addList").unbind().on('click', function () {
            site.send({
                url: "manager/insertContractNo.do",
                //async:false
                data: frontData,
                callback: function (data) {
                    var list = JSON.parse(data)
                    if (list.type == '1') {
                        all_select();
                        Constant.fn.Message('T', list.msg);
                    } else {
                        Constant.fn.Message('F', list.msg);
                    }

                }
            })
        })
        // select操作
        $(".invalid_num_select").unbind().on('change', function () {
            var obj = $(this).find("option:selected");
            order_id = $(this).data('id')
            if (obj.val() == 'noNo') {
                $(".invalid_num_winBox").show();
                $(".invalid_num_select").val('-1');
                updateData.id = $(this).data('id');
                updateData.status = 2;   //作废传2
            }
            if (obj.val() == 'recoverNo') {
                $(".valid_num_winBox").show();
                $(".invalid_num_select").val('-1');
                recoverData.id = $(this).data('id');
                recoverData.status = 1;  //恢复传1
                //recoverData.id = $(this).data('id');
            }
            if (obj.val() == 'relatedOrder') {
                //alert('关联订单')
                pjaxGo('loanIng/contractManager/detail.html?order_id=' + $(this).data('oid') + "&product_id=" + $(this).data('pid'));
            }
            if (obj.val() == 'submitVisit') {
                Common.visit(order_id);
                Common.init(3, order_id);
            }
            //if()
            $(this).val('-1')
        })
        //拿到所有的复选框，当有复选框没选上的时候,取消全选
        $("input:checkbox[name='ch_id']").unbind().on('click', function () {
            var checkboxs = $("input:checkbox[name='ch_id']");
            var sum = 0;
            var length = checkboxs.length;
            $.each(checkboxs, function (i, checkbox) {
                if ($(checkbox).prop("checked")) {
                    sum++;
                }
            })
            if (sum == length) {
                $('.all_checked').prop("checked", true);
            } else {
                $('.all_checked').prop("checked", false);
            }
        })
        // 全选
        $(".all_checked").unbind().click(function () {
            $("input[name='ch_id']").prop('checked', $(this).prop('checked'))
        })


        // 批量操作 作废编号
        $(".all_invalid_num").unbind().on('change', function () {
            var obj = $(this).find("option:selected").val();
            if (obj == '1') {
                updateData.id = '';
                updateData.status = '';
                var inp = $("input[name='ch_id']");//获得所有check按钮
                var count = 0;//计数
                for (var i = 0; i < inp.length; i++) {
                    if (inp[i].checked) {
                        count++;
                        if (count < 2) {
                            updateData.id = inp[i].value;
                            updateData.status = 2;
                        } else {
                            updateData.id += ("," + inp[i].value);
                            updateData.status = 2;
                        }
                    }
                }
                if (count != 0) {
                    updateContractNoFun();
                } else {
                    Constant.fn.Message("I", "未选择")
                }
            }
            if (obj == '2') {
                recoverData.id = '';
                recoverData.status = '';
                var inp = $("input[name='ch_id']");//获得所有check按钮
                var count = 0;//计数
                for (var i = 0; i < inp.length; i++) {
                    if (inp[i].checked) {
                        count++;
                        if (count < 2) {
                            recoverData.id = inp[i].value;
                            recoverData.status = 1;
                        } else {
                            recoverData.id += ("," + inp[i].value);
                            recoverData.status = 1;
                        }
                    }
                }
                if (count != 0) {
                    recoverContractNoFun();
                } else {
                    Constant.fn.Message("I", "未选择")
                }
            }
            $(".all_invalid_num").val('-1')
        })
    }

    $("#commit_invalid").on('click', function () {
        updateContractNoFun();
    })


    $("#commit_valid").on('click', function () {
        recoverContractNoFun();
    })

    //日期控件
    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true
    })


// 作废编号
    function updateContractNoFun() {
        site.send({
            url: "manager/updateContractNo.do",
            //async:false
            data: updateData,
            callback: function (data) {
                var list = JSON.parse(data)
                if (list.type == '1') {
                    $(".invalid_num_winBox").hide();
                    //all_select();
                    Constant.fn.Message('T', list.msg);
                    pjaxReload();
                } else {
                    Constant.fn.Message('F', list.msg);
                }

            }
        })
    }

    //恢复编号
    function recoverContractNoFun() {
        site.send({
            url: "manager/updateContractNo.do",
            //async:false
            data: recoverData,
            callback: function (data) {
                var list = JSON.parse(data)
                if (list.type == '1') {
                    $(".valid_num_winBox").hide();
                    //all_select();
                    Constant.fn.Message('T', list.msg);
                    pjaxReload();
                } else {
                    Constant.fn.Message('F', list.msg);
                }

            }
        })
    }

    //列表搜索
    $("#contract_serach").on('click', function () {
        frontData = {
            pageNum: 1,
            state: 1
        }
        if ($("#start_time").val() != '') {
            frontData.beginTime = $("#start_time").val();
        }
        if ($("#end_time").val() != '') {
            frontData.endTime = $("#end_time").val();
        }
        if ($("#seleNames").val() != '') {
            frontData.type = $("#seleNames").val();
        }
        if ($("#inputNames").val() != '' && $("#inputNames").val().trim()) {
            frontData.name = $("#inputNames").val();
        }
        if ($("#contractStatus").val() != '') {
            frontData.status = $("#contractStatus").val();
        }
        all_select();
    })
})